package com.satan.fmmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.satan.fmmall.entity.Product;
import com.satan.fmmall.entity.ProductParams;
import com.satan.fmmall.mapper.ProductMapper;
import com.satan.fmmall.mapper.ProductParamsMapper;
import com.satan.fmmall.service.ProductParamsService;
import com.satan.fmmall.vo.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName ProductParamsServiceImpl
 * @Description TODO
 * @date 2022/4/22 12:25
 * @Version 1.0
 */
@Service
public class ProductParamsServiceImpl implements ProductParamsService {
    @Resource
    private ProductParamsMapper productParamsMapper;
    @Resource
    private ProductMapper productMapper;

        @Override
        public ResultVo listBrands(Integer categoryId) {
            LambdaQueryWrapper<Product> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(Product::getCategoryId,categoryId);
            return getBrands(queryWrapper);
        }

    @Override
    public ResultVo listBrands(String keyword) {
        LambdaQueryWrapper<Product> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(Product::getProductName,keyword);
        return getBrands(queryWrapper);

    }

    public ResultVo getBrands(LambdaQueryWrapper queryWrapper){
        List<Product> products = productMapper.selectList(queryWrapper);
        if (products.size()==0){
            return ResultVo.success(null);
        }
        List<String> productList = products.stream().map(Product::getProductId).collect(Collectors.toList());
        LambdaQueryWrapper<ProductParams> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.in(ProductParams::getProductId,productList);
        List<ProductParams> productParams = productParamsMapper.selectList(queryWrapper1);
        List<String> brands = productParams.stream().map(ProductParams::getBrand).distinct().collect(Collectors.toList());
        return ResultVo.success(brands);
    }
}
